$(document).ready(function () {
    /********************************一级菜单******************************************/
    //切换一级菜单  样式
    first_menu_switch();
    function first_menu_switch() {
        var nav = $('#sidebar-nav');
        var first_menu = nav.find('.sidebar-nav-a');
        nav.delegate('.sidebar-nav-a','click',function () {
            var img_nav = $(this).find('.define-nav');//获取导航图片
            var img_rotate = $(this).find('.define-left');//获取导航方向图片
            var span = $(this).find('span').text();//获取菜单文本
            var ul = $(this).next('ul');//获取一级下拉菜单

            //修改文本选中样式
            first_menu.removeClass('define-active');//删除所有一级菜单选中样式
            $(this).addClass('define-active');//当前项添加选中样式

            //修改选中及非选中  图片替换
            nav.find('.define-nav').each(function (index,item) {
                first_menu_switch_img_unselect($(item),$(item).next().text());
            });//取消所有选中图片
            first_menu_switch_img_select(img_nav,span);//切换一级菜单  导航图片  选中

            //添加、删除旋转动画
            first_menu_switch_img_rotate(img_rotate,ul);

            first_menu_close_all();//一级菜单下拉菜单关闭

            $('#sidebar-nav').find('.active-three').removeClass('define-active');//删除所有三级菜单选中样式
        });
    }

    //切换一级菜单  导航图片  选中
    function first_menu_switch_img_select(img_nav,span) {
        if(span == '医院内部管理'){
            img_nav.attr('src','/statics/images/sever_hospital_select.png');
        }
        if(span == '服务中心'){
            img_nav.attr('src','/statics/images/sever_center_select.png');
        }
        if(span == '预警管理'){
            img_nav.attr('src','/statics/images/sever_warning_select.png');
        }
        if(span == '投诉管理'){
            img_nav.attr('src','/statics/images/sever_complaint_select.png');
        }
        if(span == '后勤管理'){
            img_nav.attr('src','/statics/images/secer_logi_select.png');
        }
        if(span == '资产管理'){
            img_nav.attr('src','/statics/images/secer_property_select.png');
        }
        if(span == '公告管理'){
            img_nav.attr('src','/statics/images/sever_announcements_select.png');
        }
        if(span == '管理账号管理'){
            img_nav.attr('src','/statics/images/sever_account_select.png');
        }
    }

    //切换一级菜单  导航图片  取消选中
    function first_menu_switch_img_unselect(img_nav,span) {
        if(span == '医院内部管理'){
            img_nav.attr('src','/statics/images/sever_hospital.png');
        }
        if(span == '服务中心'){
            img_nav.attr('src','/statics/images/sever_center.png');
        }
        if(span == '预警管理'){
            img_nav.attr('src','/statics/images/sever_warning.png');
        }
        if(span == '投诉管理'){
            img_nav.attr('src','/statics/images/sever_complaint.png');
        }
        if(span == '后勤管理'){
            img_nav.attr('src','/statics/images/secer_logi.png');
        }
        if(span == '资产管理'){
            img_nav.attr('src','/statics/images/secer_property.png');
        }
        if(span == '公告管理'){
            img_nav.attr('src','/statics/images/sever_announcements.png');
        }
        if(span == '管理账号管理'){
            img_nav.attr('src','/statics/images/sever_account.png');
        }
    }

    //切换一级菜单  导航图片  旋转动画
    function first_menu_switch_img_rotate(img_rotate,ul) {
        if(img_rotate.length > 0){
            if(ul.hasClass('in')){
                nav_img_rotate_remove(img_rotate);//菜单栏最右边图片删除动画
            }else {
                nav_img_rotate_add(img_rotate);//菜单栏最右边图片删除动画
            }
        }
    }

    //一级菜单下拉菜单关闭
    function first_menu_close_all() {
        var nav = $('#sidebar-nav');
        var first_ul = nav.find('.accounts-menu-first-ul');
        first_ul.each(function (index,item) {
            var parent = $(item).prev();
            var img_rotate = parent.find('.define-left');
            if( !parent.hasClass('define-active') && $(item).hasClass('in') ){
                $(item).collapse('hide');
                nav_img_rotate_remove(img_rotate);//菜单栏最右边图片删除动画
            }

            close_three_menu_in_first_menu(first_ul.find('ul'),first_ul.children('li'));//关闭一级菜单  同时关闭二级菜单下的三级菜单
        });
    }

    //关闭一级菜单  同时关闭二级菜单下的三级菜单  并删除选中样式
    function close_three_menu_in_first_menu(three_ul,two_li) {
        three_ul.each(function (index,item) {
            var img_rotate = $(item).parent().find('.define-left');
            if($(item).hasClass('in')){
                $(item).collapse('hide');//关闭所有三级菜单
                nav_img_rotate_remove(img_rotate);//移除二级菜单下的三级菜单动画
            }
        });
        two_li.removeClass('define-active');
    }

    /********************************二级菜单******************************************/
    //二级菜单样式转换
    second_menu_switch();
    function second_menu_switch() {
        $('#sidebar-nav').delegate('.accounts-menu-two-ul','click',function () {
            var ul_first_menu = $(this).parents('ul');//获取一级菜单
            var ul_three_menu = $(this).parent().find('ul');//获取三级菜单
            var li_two_menu_list = ul_first_menu.children('li');//获取当前二级菜单  所有二级菜单li
            var li_two_menu_item = $(this).parent();//获取当前项  当前二级菜单li
            var img_rotate = $(this).next();//获取导航旋转图片    当前二级菜单li中可旋转图片

            li_two_menu_list.removeClass('define-active');//当前二级菜单列表删除活动样式
            li_two_menu_item.addClass('define-active');//当前点击项添加活动样式

            if(img_rotate.length > 0){
                if(ul_three_menu.hasClass('in')){
                    nav_img_rotate_remove(img_rotate);//菜单栏最右边图片删除动画
                }else {
                    nav_img_rotate_add(img_rotate);//菜单栏最右边图片添加动画
                }
            }

            second_menu_close_all(li_two_menu_list);

            $('#sidebar-nav').find('.active-three').removeClass('define-active');//删除所有三级菜单选中样式
        });
    }

    //关闭二级菜单下的三级菜单
    function second_menu_close_all(li_two_menu_list) {
        li_two_menu_list.each(function (index,item) {
            var ul_three = $(item).find('ul');
            if(ul_three.hasClass('in') && !$(item).hasClass('define-active')){
                ul_three.collapse('hide');
                nav_img_rotate_remove($(item).find('.define-left'));//菜单栏最右边图片删除动画
            }
        });
    }

    /****************************** 一级 && 二级菜单公用方法 ***********************/
    //菜单栏最右边图片添加动画
    function nav_img_rotate_add(img_rotate) {
        img_rotate.removeClass('define-rotate-restore');//删除还原动画
        img_rotate.addClass('define-rotate-active');//添加动画
    }

    //菜单栏最右边图片删除动画
    function nav_img_rotate_remove(img_rotate) {
        img_rotate.removeClass('define-rotate-active');//删除动画
        img_rotate.addClass('define-rotate-restore');//添加还原动画
    }

    /********************************三级菜单******************************************/
    third_menu_switch();
    function third_menu_switch(){
        $('#sidebar-nav').delegate('.active-three','click',function () {
            $('#sidebar-nav').find('.active-three').removeClass('define-active');
            $(this).addClass('define-active');
        });
    }


    /*页面跳转*/
    skip_page();
    function skip_page() {
        //科室管理
        $('#accounts-menu-first-01 a[href="#accounts-menu-second-01"]').on('click',function () {
            window.location.href = '/hospital/depart'
        });
        //人员管理
        $('#accounts-menu-first-01 a[href="#accounts-menu-second-02"]').on('click',function () {
            window.location.href = '/hospital/hos_user'
        });
        //患者账号管理
        $('#accounts-menu-second-03').find('li').eq(0).on('click',function () {
            window.location.href = '/patient/patient'
            // window.location.href = '/user/patient_management'
        });
        //腕带管理
        $('#accounts-menu-second-03').find('li').eq(1).on('click',function () {
            window.location.href = '/patient/wristlet_management'
        });
        //黑名单管理  黑名单管理
        $('#accounts-menu-second-04').find('li').eq(0).on('click',function () {
            window.location.href = '/patient/blacklist_management'
        });
        //黑名单管理  权限管理
        $('#accounts-menu-second-04').find('li').eq(1).on('click',function () {
            window.location.href = '/patient/limits_management'
        });
        //患者呼叫
        $('#accounts-menu-first-02 a[href="#accounts-menu-second-01"]').on('click',function () {
            window.location.href = '/user/patient_call_management'
        });
        //院内呼叫
        $('#accounts-menu-first-02 a[href="#accounts-menu-second-02"]').on('click',function () {
            window.location.href = '/user/hospital_call_management'
        });
        //预警处理
        $('#accounts-menu-first-03 a[href="#accounts-menu-second-01"]').on('click',function () {
            window.location.href = '/alarm/warning_deal_management'
        });
        //报警信号
        $('#accounts-menu-first-03 a[href="#accounts-menu-second-02"]').on('click',function () {
            window.location.href = '/alarm/warning_record_management'
        });
        //报警卡绑定记录
        $('#accounts-menu-first-03 a[href="#accounts-menu-second-04"]').on('click',function () {
            window.location.href = '/alarm/warning_bind_management'
        });
        //患者投诉  投诉汇总
        $('#accounts-menu-second-04-01').find('li').eq(0).on('click',function () {
            window.location.href = '/user/patient_all_complain_management'
        });
        //患者投诉  投诉参数设置
        $('#accounts-menu-second-04-01').find('li').eq(1).on('click',function () {
            window.location.href = '/user/patient_parameter_management'
        });
        //院内投诉  投诉汇总
        $('#accounts-menu-second-04-02').find('li').eq(0).on('click',function () {
            window.location.href = '/user/hospital_all_complain_management'
        });
        //院内投诉  投诉参数设置
        $('#accounts-menu-second-04-02').find('li').eq(1).on('click',function () {
            window.location.href = '/user/hospital_parameter_management'
        });
        //后勤管理
        $('a[href="#accounts-menu-first-05"]').on('click',function () {
            window.location.href = '/user/logistics_management'
        });
        //资产管理
        $('a[href="#accounts-menu-first-06"]').on('click',function () {
            //window.location.href = '/user/'
        });
        //公告管理
        $('a[href="#accounts-menu-first-07"]').on('click',function () {
            window.location.href = '/notice/notice';
	        // window.location.href = '/user/notice_management'
        });
        //管理账号管理
        $('a[href="#accounts-menu-first-08"]').on('click',function () {
            window.location.href = '/user/admin'
        });
    }
});